/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.car;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.car.Car;
import com.unis.service.attach.AttachLinkService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-01-11
 */
@Service("carService")
public class CarServiceImpl implements CarService {

    private IDataSet4iBatis iDataSet4iBatis;
    private AttachLinkService attachlinkService;


    /****************************************
     * Insert a instace of Car
     ****************************************/
    public void insert(Car car) throws Exception {
        car.setCarid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(car);
        // 车辆表与附件表进行关联 如果有多个附件则循环插入
        if (!"".equals(car.getCartimage())) {
            AttachLink attachlink = new AttachLink();
            attachlink.setStatmentId("AttachLink.insert");
            attachlink.setEntityid(car.getCarid());
            String[] fileids = car.getCartimage().split(",");
            for (int i = 0; i < fileids.length; i++) {
                attachlink.setFileid(fileids[i]);
                attachlinkService.insert(attachlink);
            }
        }
    }

    /****************************************
     * Update a instace of Car
     ****************************************/
    public void update(Car car) throws Exception {
        iDataSet4iBatis.update(car);
    }

    /****************************************
     * Delete a instace of Car
     ****************************************/
    public void delete(Car car) throws Exception {
        iDataSet4iBatis.delete(car);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Car car) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Car car) throws Exception {
        String statementId = car.getStatmentId();
        String mainId = car.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> carList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Car model = new Car();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            carList.add(model);
        }
        iDataSet4iBatis.batchDelete(carList);
    }

    /****************************************
     * Query instaces of Car
     ****************************************/
    public List<BaseModel> queryAsList(Car car) throws Exception {
        String queryListStatement = "";
        if (car.isPaging()) {
            queryListStatement = car.getStatmentId();
            car.setStatmentId("Car.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(car);
            car.setTotalCount(baseModel.getTotalCount());
            car.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(car);
    }

    /****************************************
     * Query a instaces of Car
     ****************************************/
    public BaseModel queryAsObject(Car car) throws Exception {
        return iDataSet4iBatis.queryAsObject(car);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }
}
